package com.CNMedical.system.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.diboot.core.binding.Binder;
import com.diboot.core.service.impl.BaseServiceImpl;
import com.CNMedical.common.utils.DateUtils;
import com.CNMedical.system.domain.vo.AppletKnowledgeVO;
import com.CNMedical.system.service.IAppletExamScoreService;
import com.CNMedical.system.service.IAppletKnowledgeCollectService;
import org.springframework.stereotype.Service;
import com.CNMedical.system.mapper.AppletKnowledgeMapper;
import com.CNMedical.system.domain.entity.AppletKnowledge;
import com.CNMedical.system.service.IAppletKnowledgeService;

import javax.annotation.Resource;

/**
 * 知识信息Service业务层处理
 * 
 * @author tang
 * @date 2023-03-28
 */
@Service
public class AppletKnowledgeServiceImpl extends BaseServiceImpl<AppletKnowledgeMapper, AppletKnowledge> implements IAppletKnowledgeService
{
    @Resource
    private AppletKnowledgeMapper appletKnowledgeMapper;

    @Resource
    private IAppletKnowledgeCollectService appletKnowledgeCollectService;

    @Resource
    private IAppletExamScoreService appletExamScoreService;


    @Override
    public AppletKnowledgeVO appGetKnowledgeDetail(String knowledgeId,String userId) {
        AppletKnowledge appletKnowledge = lambdaQuery().eq(AppletKnowledge::getId, knowledgeId).one();
        AppletKnowledgeVO appletKnowledgeVO = BeanUtil.toBean(appletKnowledge, AppletKnowledgeVO.class);
        appletKnowledgeVO.setIsCollect(appletKnowledgeCollectService.isCollect(knowledgeId, userId));
        appletKnowledgeVO.setIsExam(appletExamScoreService.isExam(knowledgeId, userId));
        return appletKnowledgeVO;
    }

    @Override
    public List<AppletKnowledge> selectAppletKnowledgeByTypeId(String typeId) {
        return lambdaQuery().eq(AppletKnowledge::getTypeId,typeId).list();
    }

    @Override
    public List<AppletKnowledge> searchKnowLedge(String content) {
        List<AppletKnowledge> appletKnowledgeVoList = appletKnowledgeMapper.searchKnowLedge(content);
        return appletKnowledgeVoList;
    }

    /**
     * 查询知识信息
     * 
     * @param id 知识信息主键
     * @return 知识信息
     */
    @Override
    public AppletKnowledge selectAppletKnowledgeById(Long id)
    {
        return appletKnowledgeMapper.selectAppletKnowledgeById(id);
    }

    /**
     * 查询知识信息列表
     * 
     * @param appletKnowledge 知识信息
     * @return 知识信息
     */
    @Override
    public List<AppletKnowledgeVO> selectAppletKnowledgeList(AppletKnowledge appletKnowledge)
    {
        List<AppletKnowledge> appletKnowledges = appletKnowledgeMapper.selectAppletKnowledgeList(appletKnowledge);
        List<AppletKnowledgeVO> appletKnowledgeVOS = Binder.convertAndBindRelations(appletKnowledges, AppletKnowledgeVO.class);

        return appletKnowledgeVOS;
    }

    /**
     * 新增知识信息
     * 
     * @param appletKnowledge 知识信息
     * @return 结果
     */
    @Override
    public int insertAppletKnowledge(AppletKnowledge appletKnowledge)
    {
        appletKnowledge.setCreateTime(DateUtils.getNowDate());
        return appletKnowledgeMapper.insertAppletKnowledge(appletKnowledge);
    }

    /**
     * 修改知识信息
     * 
     * @param appletKnowledge 知识信息
     * @return 结果
     */
    @Override
    public int updateAppletKnowledge(AppletKnowledge appletKnowledge)
    {
        appletKnowledge.setUpdateTime(DateUtils.getNowDate());
        return appletKnowledgeMapper.updateAppletKnowledge(appletKnowledge);
    }

    /**
     * 批量删除知识信息
     * 
     * @param ids 需要删除的知识信息主键
     * @return 结果
     */
    @Override
    public int deleteAppletKnowledgeByIds(Long[] ids)
    {
        return appletKnowledgeMapper.deleteAppletKnowledgeByIds(ids);
    }

    /**
     * 删除知识信息信息
     * 
     * @param id 知识信息主键
     * @return 结果
     */
    @Override
    public int deleteAppletKnowledgeById(Long id)
    {
        return appletKnowledgeMapper.deleteAppletKnowledgeById(id);
    }
}
